package IO.io;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
    @Test
    public void test(){
        RandomAccessFile raf=null;
        try {
            raf=new RandomAccessFile(new File("hello.txt"),"rw");
            raf.seek(3);
            StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
            byte[] bytes=new byte[20];
            int len;
            while ((len=raf.read(bytes))!=-1){
                builder.append(new String(bytes,0,len));
            }
            raf.seek(3);
            raf.write("kxb".getBytes());
            raf.write(builder.toString().getBytes());

        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
